package MyApp.Model.proxy
{
	import MyApp.Model.Well;
	import MyApp.Model.WellGroup;
	import MyApp.Model.WellObsRecord;
	import MyApp.Model.WellPrdRecord;
	
	import components.well.WellContext;
	
	import custom.pager.Pager;
	
	import mx.controls.Alert;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import util.Constants;
	import util.GDUtil;
	
	public class WellProxy extends Proxy
	{
		public static const NAME:String=GDUtil.getUID();
		public static const ERROR_MESSAGE_RETURN:String=GDUtil.getUID();
		public static const WELL_LOAD_OK:String=GDUtil.getUID();
		public static const SAVE_WELL_OK:String=GDUtil.getUID();
		public static const OBS_RECORD_LOAD_OK:String=GDUtil.getUID();
		public static const ADD_OBS_RECORD_OK:String=GDUtil.getUID();
		public static const UPDATE_OBS_RECORD_OK:String=GDUtil.getUID();
		public static const PRD_RECORD_LOAD_OK:String=GDUtil.getUID();
		public static const ADD_PRD_RECORD_OK:String=GDUtil.getUID();
		public static const UPDATE_PRD_RECORD_OK:String=GDUtil.getUID();
		public function WellProxy(proxyName:String=null, data:Object=null)
		{
			super(proxyName, data);
		}
		private var remote:RemoteObject;
		protected function get wellService():RemoteObject{
			if(remote==null){
				remote=GDUtil.remoteObject();
				remote.destination = Constants.SER_WELL;
			}
			return remote;
		}
		public function loadWellList():void{
			var ope:AbstractOperation=wellService.getOperation("findAll");
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(WELL_LOAD_OK,e.result);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send();
		}
		public function doSaveWell(w:Well):void{
			var ope:AbstractOperation=wellService.getOperation("saveWell");
			var hd:Function=function (e:ResultEvent):void{
				Alert.show("保存成功！");
				facade.sendNotification(SAVE_WELL_OK);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(w);
		}
		public function loadObsRecordList(pager:Pager):void{
			var ope:AbstractOperation=wellService.getOperation("findObsReordByPage");
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(OBS_RECORD_LOAD_OK,e.result);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(WellContext.well.id,pager);
		}
		public function addWellObsRecord(record:WellObsRecord):void{
			var ope:AbstractOperation=wellService.getOperation("addObsRecord");
			var hd:Function=function (e:ResultEvent):void{
				Alert.show("保存成功！");
				facade.sendNotification(ADD_OBS_RECORD_OK);
				loadObsRecordList(new Pager());
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(record);
		}
		public function saveWellObsRecord(obj:Object):void{
			var record:WellObsRecord=obj.record as WellObsRecord;
			var pager:Pager=obj.pager as Pager;
			var ope:AbstractOperation=wellService.getOperation("saveObsRecord");
			var hd:Function=function (e:ResultEvent):void{
				Alert.show("保存成功！");
				facade.sendNotification(UPDATE_OBS_RECORD_OK);
				loadObsRecordList(pager);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(record);
		}
		public function deleteWellObsRecord(obj:Object):void{
			var recordId:int=obj.recordId;
			var pager:Pager=obj.pager as Pager;
			var ope:AbstractOperation=wellService.getOperation("deleteObsRecord");
			var hd:Function=function (e:ResultEvent):void{
				loadObsRecordList(pager);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(recordId);
		}
		public function loadPrdnRecordList():void{
			var d:Date=WellContext.queryDate;
			var wg:WellGroup=WellContext.wellGroup;
			var ope:AbstractOperation=wellService.getOperation("findProductionRecord");
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(WellProxy.PRD_RECORD_LOAD_OK,e.result);
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(d,wg.wellGroupId);
		}
		public function addWellPrdRecord(record:WellPrdRecord):void{
			var ope:AbstractOperation=wellService.getOperation("addPrdRecord");
			var hd:Function=function (e:ResultEvent):void{
				Alert.show("保存成功！");
				facade.sendNotification(ADD_PRD_RECORD_OK);
				loadPrdnRecordList();
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(record);
		}
		public function updateWellPrdRecord(record:WellPrdRecord):void{
			var ope:AbstractOperation=wellService.getOperation("savePrdRecord");
			var hd:Function=function (e:ResultEvent):void{
				Alert.show("保存成功！");
				facade.sendNotification(UPDATE_PRD_RECORD_OK);
				loadPrdnRecordList();
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(record);
		}
		public function deleteWellPrdRecord(id:int):void{
			var ope:AbstractOperation=wellService.getOperation("deletePrdRecord");
			var hd:Function=function (e:ResultEvent):void{
				loadPrdnRecordList();
				ope.removeEventListener(ResultEvent.RESULT,hd);
			};
			ope.addEventListener(ResultEvent.RESULT,hd);
			ope.addEventListener(FaultEvent.FAULT,listFault);
			ope.send(id);
		}
		private function listFault(e:FaultEvent):void{
			this.sendNotification(ERROR_MESSAGE_RETURN,{message:"数据获取失败"});
			Alert.show("数据获取失败");
		}
	}
}